﻿using System;

namespace RStein.Posterous.API
{
    internal class TwitterAccount : ITwitterAccount, IEquatable<TwitterAccount>
    {

        #region constructors

        public TwitterAccount(IPosterousApplication application, string userName, string password)
        {
            if (application == null)
            {
                throw new ArgumentNullException("application");
            }

            if (String.IsNullOrEmpty(userName))
            {
                throw new ArgumentException("userName");
            }

            if (String.IsNullOrEmpty(userName))
            {
                throw new ArgumentException("password");
            }

            Application = application;
            UserName = userName;
            Password = password;
        }

        #endregion constructors

        #region methods

        public bool Equals(TwitterAccount other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }
            return Equals(other.UserName, UserName);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
            if (obj.GetType() != typeof (TwitterAccount))
            {
                return false;
            }
            return Equals((TwitterAccount) obj);
        }

        public override int GetHashCode()
        {
            return (UserName != null ? UserName.GetHashCode() : 0);
        }

        public static bool operator ==(TwitterAccount left, TwitterAccount right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(TwitterAccount left, TwitterAccount right)
        {
            return !Equals(left, right);
        }

        #endregion methods
        #region Implementation of ITwitterAccount

        public string UserName
        { 
            get; 
            set;
        }
        
        public string Password
        {
            get;
            set;
        }

        public ITwitterPost CreatePost(string title, string body, bool postToTwitter)
        {
            return new TwitterPost(this)
                       {
                           AutopostToTwitter = postToTwitter,
                           Body = body ?? String.Empty,
                           Title = title ?? String.Empty
                       };
        }

        

        #endregion

        #region Implementation of IApplicationHolder

        public IPosterousApplication Application
        {
            get;
            set;
        }

        #endregion
    }
}